//
//  FQManualCerificationViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/22.
//

import UIKit
import RxCocoa

class FQManualCerificationViewController: BaseViewController {

    let buttonIsSelected = BehaviorRelay(value: true)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .light
    }
    
    override func initSubviews() {
        super.initSubviews()
        
        let titleLb = QMUILabel(font: .mSFont(21), textColor: .color51)
        titleLb.text = "填写本人实名信息"
        view.addSubview(titleLb)
        
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(NavigationContentTop + 32)
        }
        
        let subTitleLb = QMUILabel(font: .rSFont(13), textColor: .color149)
        subTitleLb.text = "用于身份实名核验、反洗钱及额度评估，资料将被加密保护"
        subTitleLb.numberOfLines = 0
        view.addSubview(subTitleLb)
        
        subTitleLb.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(titleLb.snp.bottom).offset(10)
            make.right.equalTo(-20)
        }
        
        let inputView = FQCerificationInputView(1)
        view.addSubview(inputView)
        
        inputView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(inputView.qmui_height)
            make.top.equalTo(subTitleLb.snp.bottom).offset(32)
        }
        
        let bottomView = FQAuthBottonView()
        bottomView.backgroundColor = .clear
        view.addSubview(bottomView)
        
        bottomView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(90)
            make.top.equalTo(inputView.snp.bottom).offset(22)
        }
        
        bottomView.toggleBlock = { [unowned self] isSelected in
            self.buttonIsSelected.accept(isSelected)
        }
        
        bottomView.buttonBlock = { [unowned self] in
            if inputView.nameTF.text!.isEmpty {
                QMUITips.showError(inputView.nameTF.placeholder)
                return
            }
            if inputView.idTF.text!.isEmpty {
                QMUITips.showError(inputView.idTF.placeholder)
                return
            }
            let aesName = WMEncryptTool.manager.aesEncrypt(inputView.nameTF.text!)
            let aesIdCard = WMEncryptTool.manager.aesEncrypt(inputView.idTF.text!)
            NetworkManager.manager.request(targetType: .idcardAudit(name: aesName, idCard: aesIdCard), responseModelType: NetworkBaseModel<FQIDCardModel>.self) { responseModel in
                if responseModel.code != .successCode {
                    QMUITips.showError(responseModel.msg)
                    return
                }
                let model = YHBaseConfiguration.shared.configModel!
                let userModel = BaseUserModel.unarchiver()!
                if model.face && !userModel.isFace {
                    let vc = FQFaceCerificationViewController()
                    vc.idCardAes = aesIdCard
                    vc.nameAes = aesName
                    vc.verificationToken = responseModel.data!.verificationToken
                    self.navigationController?.qmui_pushViewController(vc, animated: true)
                } else {
                    if model.relationship && !userModel.isAccomplishRelationship {
                        let vc = FQFillInfoViewController(.kinsfolk)
                        vc.idCardAes = aesIdCard
                        vc.nameAes = aesName
                        self.navigationController?.qmui_pushViewController(vc, animated: true)
                    } else {
                        let vc = FQFillInfoViewController(.baseInfo)
                        vc.idCardAes = aesIdCard
                        vc.nameAes = aesName
                        self.navigationController?.qmui_pushViewController(vc, animated: true)
                    }
                }
            }
        }
        
        let validName : Driver<Bool> = inputView.nameTF.rx.text.orEmpty.asDriver().map ({ text in
            text.count >= 2
        })
        
        let validId : Driver<Bool> = inputView.idTF.rx.text.orEmpty.asDriver().map ({ text in
            text.count == 18
        })
        
        Driver.combineLatest(validName, validId, buttonIsSelected.asDriver(), resultSelector: {(nameIsValid, idIsValid, buttonIsSelected) -> Bool in
            (nameIsValid && idIsValid && buttonIsSelected)
        }).distinctUntilChanged().drive(bottomView.completionBtn.rx.buttonIsEnable).disposed(by: rx.disposeBag)
    }
    
    override func setupNavigationItems() {
        super.setupNavigationItems()
        title = "身份认证"
    }
}
